package com.shengdong.water.feature.management.inspection

import android.annotation.SuppressLint
import android.webkit.JavascriptInterface
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.alibaba.android.arouter.facade.annotation.Route
import com.shengdong.water.R
import com.shengdong.water.code.base.BaseActivity
import com.shengdong.water.code.config.RouterConfig
import com.shengdong.water.databinding.ActivityMonitorMapBinding
import dagger.hilt.android.AndroidEntryPoint

@Route(path = RouterConfig.Management.MONITOR_MAP_PATH)
@AndroidEntryPoint
class MonitorMapActivity: BaseActivity<ActivityMonitorMapBinding>() {
    override fun getLayoutId() = R.layout.activity_monitor_map

    @SuppressLint("SetJavaScriptEnabled")
    override fun initContentView() {
        super.initContentView()
        setTitle("监测点地图查看")

        binding.tvName.text = intent.getStringExtra("name")
        binding.tvCode.text = buildString {
        append("排口编码      ")
        append(intent.getStringExtra("code"))
    }
        binding.tvAddress.text = buildString {
            append("排口地址      ")
            append(intent.getStringExtra("address"))
        }
        binding.tvLanglat.text = buildString {
            append("排口经纬度      ")
            append(intent.getStringExtra("langlat"))
        }
        binding.tvSign.text = buildString {
            append("签到定位      ")
            append(intent.getStringExtra("signLangLat"))
        }

        val webView = binding.web
        WebView.setWebContentsDebuggingEnabled(true)
        val webSettings = webView.settings
        webSettings.javaScriptEnabled =true
        webSettings.domStorageEnabled = true
        webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW

        // 添加JavaScript接口
        webView.addJavascriptInterface(WebAppInterface(), "Android");
        webView.webViewClient = WebViewClient()
        webView.loadUrl("file:///android_asset/tianditu_map.html")
        val langlat =intent.getStringExtra("langlat")
        val lang = langlat?.split(",")?.get(0)
        val lat = langlat?.split(",")?.get(1)

        binding.web.postDelayed(object : Runnable {
            override fun run() {
                binding.web.evaluateJavascript("javascript:initMap($lang, $lat)",null)
            }
        },2000)
//        binding.web.evaluateJavascript("javascript:initMap($lang, $lat)",null)
//        binding.web.evaluateJavascript("javascript:initMap(${langlat?.split(",")[0]}, ${langlat?.split(",")[1]})",null)

    }




    inner class WebAppInterface() {
        @JavascriptInterface
        fun onFenceClick(json: String) {
        }
    }



}